/**
 * 工具类统一暴露接口
 */
const formatTime = require('./formatTime.js');
const port = require('./port.js');
const log = require('./log.js');
const createValidate = require('./createValidate.js');
const storage = require('./storage.js');

const webView = require('./webView.js');

import wxValidate from './WxValidate';

const Promise = require('../plugins/es6-promise.js')

//封装Request请求方法
// function requst(url, method, data = {}) {
//   wx.showNavigationBarLoading()
//   data.method = method
//   return new Promise((resove, reject) => {
//     wx.request({
//       url: url,
//       data: data,
//       header: {},
//       method: method.toUpperCase(), // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
//       success: function (res) {
//         wx.hideNavigationBarLoading()
//         resove(res.data)
//       },
//       fail: function (msg) {
//         console.log('reqest error', msg)
//         wx.hideNavigationBarLoading()
//         reject('fail')
//       }
//     })
//   })
// }
/**
 * post 方法，接受params参数对象
 */
function post(params) {
  let url = params.url,
    header = params.header || {},
    data = params.data;

  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      data: data,
      header: header,
      method: 'POST',
      success: function (data) {
        resolve({ data })
      },
      fail: function () {
        reject('请求失败，请重试！')
      }
    })
  })
}
// promise封装原有的getUserInfo的函数
// getUserInfo() {
//   return new Promise((resolve, reject) => {
//     if (this.globalData.userInfo) {
//       resolve(this.globalData.userInfo)
//     }
//     return getUserInfo().then(res => {
//       resolve(this.globalData.userInfo = res.userInfo)
//     })
//   })
// },
function json2Form(json) {
  var str = [];
  for (var p in json) {
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  }
  return str.join("&");
}
module.exports = {
  formatTime: formatTime.formatTime,
  getDate: formatTime.getDate,
  replace: formatTime.replace,

  getPort: port.getPort,
  uploadImg: port.uploadImg,
  getImagePrefixHost: port.getImagePrefixHost,
  request: port.request,

  log: log.print_log,

  wxValidate: wxValidate,
  createValidate: createValidate,

  getStorageValueByKey: storage.getStorageValueByKey,

  bulid_query: webView.bulid_query,
  getWebViewConfig: webView.get_webview_config,
  getCenterConfig: webView.get_center_config,
  showCenterPage: webView.show_center_page,

  json2Form: json2Form,
  post: post
}